package com.zte.cl.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 系统常量
 *
 */
public class ConstantUtil {
	
	private static final Log log = LogFactory.getLog(ConstantUtil.class);
	
	private ConstantUtil(){}
	
	private static ConstantUtil instance;
	
	public static ConstantUtil getInstance() {
		if(instance == null) {
			synchronized (ConstantUtil.class) {
				if(instance == null) {
					instance = new ConstantUtil();
				}
			}
		}
		
		return instance;
	}
	
	/**
	 * 对应ehcache.xml配置的cache name
	 */
	public static String CACHE_NAME;
	
	/**
	 * 爬虫相关
	 */
	public static int SITE_SLEEP_TIME;
	public static String SITE_USER_AGENT;
	public static String PAGE_CONTENT_PATH;
	
	/**
	 * 图片相关
	 */
	public static String IMG_PATH;
	public static String IMG_SERVER;
	public static boolean IMG_COMPRESS;
	public static int IMG_MAX_WIDTH;
	public static boolean IMG_THUMBNAIL_COMPRESS;
	public static int IMG_THUMBNAIL_MAX_WIDTH;
	public static int IMG_THUMBNAIL_MIN_WIDTH;
	public static int IMG_THUMBNAIL_MIN_HEIGHT;
	public static int CRAW_CONTENT_MAX_FAILTIME;
	
	/**
	 * 代理相关
	 */
	public static boolean IF_PROXY;
	public static String PROXY_HOST;
	public static int PROXY_PORT;
	
	/**
	 * 推送的新闻默认图片
	 */
	public static String NEWS_FIRST_DEFAULT_IMG;
	public static String NEWS_DEFAULT_IMG;
	
	public static String NEWS_KEYWORD;
	public static int NEWS_DELETE_DAYS_BEFORE;
	
	/**
	 * 推送相关设置
	 */
	public static String NEWS_PUSH_INTERFACE_URL;
	public static String NEWS_PUSH_RANGE;
	public static String NEWS_PUSH_RECEIVER_CODE;
	public static String NEWS_PUSH_DEFAULT_PUBLIC_ACCOUNT;
	public static int NEWS_PUSH_THREAD_COMPANY_MAX_COUNT;
	public static int NEWS_PUSH_THREAD_ERROR_SLEEP_TIME;
	public static int NEWS_PUSH_THREAD_ERROR_ATTEMPT_COUNT;
	public static double NEWS_MAX_SIMILAR_DEGREE;
	public static String NEWS_KEYWORD_BLACKLIST;
	public static boolean NEWS_PUSH_DO;
	
	
	static {
    	log.debug("初始化常量...");
    	
    	InputStream in = ConstantUtil.class.getResourceAsStream("/constant.properties");
        
        Properties prop = new Properties();
        try {
            prop.load(new InputStreamReader(in, "UTF-8"));
            
            setupVariable(prop);
        } catch (Exception e) {
            log.error("初始化常量时发生错误！", e);
        } finally {
            try {
            	if (in != null) {
            		in.close();
            	}
            } catch (IOException e) {
            	log.error("关闭输入流时发生错误！", e);
            }
        }
    }
	
	public static void reload() {
		log.debug("reload常量...");
		Properties prop = new Properties();
		String path = ConstantUtil.class.getResource("/constant.properties").getPath(); 
		InputStream in = null;
		try {
			in = new FileInputStream(path);
			prop.load(new InputStreamReader(in, "UTF-8"));
			
			setupVariable(prop);
		} catch (Exception e) {
			log.error("reload常量时发生错误！", e);
		} finally {
			try {
            	if (in != null) {
            		in.close();
            	}
            } catch (IOException e) {
            	log.error("关闭输入流时发生错误！", e);
            }
		}
	}
	
	private static void setupVariable(Properties prop) {
		SITE_SLEEP_TIME = Integer.parseInt(prop.getProperty("site.sleeptime").trim());
        SITE_USER_AGENT = prop.getProperty("site.useragent").trim();
        PAGE_CONTENT_PATH = prop.getProperty("page.content.path").trim();
        IMG_PATH = prop.getProperty("img.path").trim();
        IMG_SERVER = prop.getProperty("img.server").trim();
        IMG_COMPRESS = Boolean.parseBoolean(prop.getProperty("img.compress").trim());
        IMG_MAX_WIDTH = Integer.parseInt(prop.getProperty("img.max.width").trim());
        IMG_THUMBNAIL_COMPRESS = Boolean.parseBoolean(prop.getProperty("img.thumbnail.compress").trim());
        IMG_THUMBNAIL_MAX_WIDTH = Integer.parseInt(prop.getProperty("img.thumbnail.max.width").trim());
        IMG_THUMBNAIL_MIN_WIDTH = Integer.parseInt(prop.getProperty("img.thumbnail.min.width").trim());
        IMG_THUMBNAIL_MIN_HEIGHT = Integer.parseInt(prop.getProperty("img.thumbnail.min.height").trim());
        CRAW_CONTENT_MAX_FAILTIME = Integer.parseInt(prop.getProperty("crawl.content.max.failtime").trim());
        CACHE_NAME = prop.getProperty("cache.name").trim();;
        
        IF_PROXY = Boolean.parseBoolean(prop.getProperty("proxy.ifProxy").trim());
        PROXY_HOST = prop.getProperty("proxy.host").trim();
        PROXY_PORT = Integer.parseInt(prop.getProperty("proxy.port").trim());
        
        NEWS_FIRST_DEFAULT_IMG = prop.getProperty("news.first.default.img").trim();
        NEWS_DEFAULT_IMG = prop.getProperty("news.default.img").trim();
        NEWS_KEYWORD = prop.getProperty("news.keyword").trim();
        NEWS_DELETE_DAYS_BEFORE = Integer.parseInt(prop.getProperty("news.delete.days.before").trim());
        
        NEWS_PUSH_INTERFACE_URL = prop.getProperty("news.push.interface.url").trim();
        NEWS_PUSH_RANGE = prop.getProperty("news.push.range").trim();
        NEWS_PUSH_RECEIVER_CODE = prop.getProperty("news.push.receiver.code").trim();
        NEWS_PUSH_DEFAULT_PUBLIC_ACCOUNT = prop.getProperty("news.push.default.publicAccount").trim();
        NEWS_PUSH_THREAD_COMPANY_MAX_COUNT = Integer.parseInt(prop.getProperty("news.push.thread.company.maxCount").trim());
        NEWS_PUSH_THREAD_ERROR_SLEEP_TIME = Integer.parseInt(prop.getProperty("news.push.thread.error.sleepTime").trim());
        NEWS_PUSH_THREAD_ERROR_ATTEMPT_COUNT = Integer.parseInt(prop.getProperty("news.push.thread.error.attemptCount").trim());
        NEWS_MAX_SIMILAR_DEGREE = Double.parseDouble(prop.getProperty("news.max.similar.degree").trim());
        NEWS_KEYWORD_BLACKLIST = prop.getProperty("news.keyword.blacklist").trim();
        NEWS_PUSH_DO = Boolean.parseBoolean(prop.getProperty("news.push.do").trim());
	}
}
